////// PRIVATE CLASSES //////
package org.osflash.signals;

import asunit.asserts.*;
import asunit.framework.IAsync;
import org.osflash.signals.events.GenericEvent;
import org.osflash.signals.events.GenericEvent;
import org.osflash.signals.events.IEvent;

class SignalWithCustomEventTest {

	@:meta(Inject())
	public var async : IAsync;
	public var messaged : Signal;
	@:meta(Before())
	public function setUp() : Void {
		messaged = new Signal(MessageEvent);
	}

	@:meta(After())
	public function tearDown() : Void {
		messaged.removeAll();
		messaged = null;
	}

	//////
	@:meta(Test())
	public function valueClasses_roundtrip_through_constructor() : Void {
		assertSame(MessageEvent, messaged.valueClasses[0]);
		assertEquals(1, messaged.valueClasses.length);
	}

	@:meta(Test())
	public function valueClasses_roundtrip_through_setter() : Void {
		messaged.valueClasses = [GenericEvent];
		assertSame(GenericEvent, messaged.valueClasses[0]);
		assertEquals(1, messaged.valueClasses.length);
	}

	@:meta(Test())
	public function valueClasses_setter_clones_the_array() : Void {
		var newValueClasses : Array<Dynamic> = [GenericEvent];
		messaged.valueClasses = newValueClasses;
		assertNotSame(newValueClasses, messaged.valueClasses);
	}

	@:meta(Test())
	public function add_one_listener_and_dispatch() : Void {
		messaged.add(async.add(onMessage, 50));
		messaged.dispatch(new MessageEvent("ok"));
	}

	function onMessage(e : MessageEvent) : Void {
		assertEquals("message value in the event", "ok", e.message);
	}

	@:meta(Test(expects="ArgumentError"))
	public function dispatch_wrong_event_type_should_throw_ArgumentError() : Void {
		messaged.dispatch(new GenericEvent());
	}

	@:meta(Test(expects="ArgumentError"))
	public function constructing_signal_with_non_class_should_throw_ArgumentError() : Void {
		new Signal(new Date());
	}

	@:meta(Test(expects="ArgumentError"))
	public function constructing_signal_with_two_nulls_should_throw_ArgumentError() : Void {
		new Signal(null, null);
	}

	@:meta(Test(expects="ArgumentError"))
	public function constructing_signal_with_class_and_non_class_should_throw_ArgumentError() : Void {
		new Signal(Date, 42);
	}

}

class MessageEvent extends GenericEvent, implements IEvent {

	public var message : String;
	public function new(message : String) {
		super();
		this.message = message;
	}

	override public function clone() : IEvent {
		return new MessageEvent(message);
	}

}

